package com.learn.project2.service.admin.collect;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketCollect;
import com.learn.project2.bean.MarketCollectExample;
import com.learn.project2.mapper.MarketCollectMapper;
import com.learn.project2.model.bo.admin.user.UserListBO;
import com.learn.project2.model.vo.admin.collect.CollectListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
//@CacheConfig(cacheNames = "marketCollect")
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    MarketCollectMapper collectMapper;
    @Override
    //@Cacheable
    public CollectListVO selectCollect(Integer userId, Integer valueId, UserListBO userListBO) {

        Integer limit = userListBO.getLimit();
        Integer page = userListBO.getPage();
        String sort = userListBO.getSort();
        String order = userListBO.getOrder();
        //使用分页查询
        PageHelper.startPage(page,limit);

        MarketCollectExample collectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = collectExample.createCriteria();
        //userId,valueId精确查询
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);
        //给查询条件
        collectExample.setOrderByClause(sort+" "+order);
        List<MarketCollect> list = collectMapper.selectByExample(collectExample);

        PageInfo<MarketCollect> pageInfo = new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return CollectListVO.data(total,pages,limit,page,list);
    }
}
